const express = require('express')
const path = require('path')
const app = express()

app.use(express.urlencoded())
app.use(express.json())

app.post('/game', (req, res) => {
  const { name, time, publisher } = req.body

  if (!name || !time || !publisher) {
    return res.send({
      code: 10001,
      msg: '添加失败',
      data: null
    })
  }

  res.send({
    code: 10000,
    msg: '添加成功',
    data: {
      '游戏名': '黑暗之魂3',
      '发行时间': '2018',
      '发行商': 'Form SoftWare'
    }
  })
})

// 静态资源接口
app.get('/1', (req, res) => {
  const filePath = path.resolve(__dirname, './3.post请求练习.html')
  res.sendFile(filePath)
})

app.listen(8080, () => {
  console.log('running');
})